package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.domain.User;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmLogin {

	protected Shell FrmLogin;
	private Text txtUserName;
	private Text txtPassword;

	/**
	 * Open the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmLogin.open();
		FrmLogin.layout();
		while (!FrmLogin.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		FrmLogin = new Shell(SWT.CLOSE);
		FrmLogin.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 13) {
					login();
				} else if (e.keyCode == 27) {
					System.exit(1);
				}
			}
		});
		FrmLogin.setSize(270, 165);
		FrmLogin.setText("用户登录");
		// 窗口居中
		FrmLogin.setLocation(new FrmHelper().setLocationCenter(FrmLogin));

		Label lblUserName = new Label(FrmLogin, SWT.NONE);
		lblUserName.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lblUserName.setBounds(57, 29, 48, 17);
		lblUserName.setText("用户名：");

		Label lblPassword = new Label(FrmLogin, SWT.NONE);
		lblPassword.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lblPassword.setBounds(57, 61, 48, 17);
		lblPassword.setText("密　码：");

		txtUserName = new Text(FrmLogin, SWT.BORDER);
		txtUserName.setBounds(111, 26, 100, 20);

		txtPassword = new Text(FrmLogin, SWT.BORDER | SWT.PASSWORD);
		txtPassword.setBounds(111, 58, 100, 20);

		Button btnLogin = new Button(FrmLogin, SWT.NONE);
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			// 用户登录
			public void mouseUp(MouseEvent e) {
				login();
			}
		});
		btnLogin.setBounds(166, 97, 88, 30);
		btnLogin.setText("登　录");

		Link link = new Link(FrmLogin, SWT.NONE);
		link.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				new FrmResetPSW().open();
			}
		});
		link.setBounds(93, 103, 67, 16);
		link.setText("<a>忘记密码？</a>");
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void login() {
		User loginUser = new User();
		User loginingUser = new User();
		// 获得用户名和密码
		loginUser.setUsername(txtUserName.getText());
		loginUser.setPassword(txtPassword.getText());
		if (loginUser.getUsername() == "") {
			new MsgBox().errorTip(FrmLogin, "用户名不能为空");
		} else if (loginUser.getPassword() == "") {
			new MsgBox().errorTip(FrmLogin, "密码不能为空");
		} else {
			try {
				loginingUser = new UserService().login(loginUser);
			} catch (Exception e) {
				e.printStackTrace();
			}
			/*
			 * int isLogined = 0; try { isLogined = new
			 * UserService().isLogined(txtUserName.getText()); } catch
			 * (Exception e) { e.printStackTrace(); }
			 */
			if (loginingUser.getId() == 0) {
				new MsgBox().errorTip(FrmLogin, "用户名或密码错误");
			}
			/*
			 * else if (isLogined == 1) { new MsgBox().errorTip(FrmLogin,
			 * "该用户已登录"); } else { try { isLogined = new
			 * UserService().isLogined("1", txtUserName.getText()); } catch
			 * (Exception e) { e.printStackTrace(); } if (isLogined != 1) { new
			 * MsgBox().errorTip(FrmLogin, "登录失败"); }
			 */else {
				// 转到主界面
				LoginedUserHelper.initLoginedUser(loginingUser);
				FrmLogin.close();
				if (LoginedUserHelper.getLoginedUserUsername().equals("loveeyesadmin")) {
					new FrmMain2().open();
				} else {
					new FrmMain1().open();
				}
			}
			/* } */
		}
	}
}